import java.awt.Point;
import java.util.Random;
import java.util.TreeMap;

public class ServerMonsterEntity extends ServerEntity {

	int tempDMG;

	Random r = new Random();

	int delay = 500, time = 0;

	double dist, tempDist;

	long timer = System.currentTimeMillis();

	ServerPlayerEntity target = new ServerPlayerEntity(null, null, null);

	public ServerMonsterEntity(MonsterEntityData data, Map map,
			ServerMessageSender ms) {
		super(data, map, ms);
		data.hp = 100;
		data.hpMax = 100;
		data.mana = 100;
		data.manaMax = 100;
		data.xp = 40;
		data.hat = 1 + r.nextInt(8);
		data.weapon = 1 + r.nextInt(8);
		double chanceToHit = (30. + (r.nextInt(10) * 5.) / 100);
	}

	public int takeDamage(int accessory, int weapon){
		try{
		boolean isHit = false;
		int damage = 0;
		double chanceToHit = .5 + ((double) (accessory / 100));
		double attRoll = r.nextDouble();
		if (attRoll <= chanceToHit)
			isHit = true;
		if (isHit) {
			damage = weapon - (data.hat / 4);
			data.hp -= damage;
			ms.monsterHealthUpdate(data.id, data.hp);
			if (data.hp <= 0) {
				data.hp=data.hpMax;
				do{
					data.location=new Point(r.nextInt(100), r.nextInt(100));
				}
				while(!map.move(this, data.location));
				ms.monsterPositionUpdate(data.id, (int)data.location.getX(), (int)data.location.getY());
				return data.xp;
			}
		}
		}catch(Exception e){
			System.out.println(e);
		}
		return 0;
	}

	public void act() {
		if (System.currentTimeMillis() - timer >= 500) {
			timer = System.currentTimeMillis();
			dist = 0;
			tempDist = 0;
			target = null;
			for (int i = (-3); i < 3; i++) {
				for (int j = (-3); j < 3; j++) {
					if (map.isValid(((int) (data.location.getX() + i)),
							((int) (data.location.getY() + j)))) {
						Object temp = map.get(((int) data.location.getX()) + i,
								((int) data.location.getY()) + j);
						if (temp instanceof ServerPlayerEntity) {
							if (target == null) {
								target = (ServerPlayerEntity) temp;
								dist = Math.hypot(i,j);
							}

							else {
								tempDist = Math.hypot(i,j);
								if (tempDist < dist)
									target = (ServerPlayerEntity) temp;

							}
							if (dist == 1)
								attack(target);
							else {
								Point newLoc = data.location;
								if (target.data.location.getX() < data.location
										.getX()) {
									newLoc = new Point(((int) data.location
											.getX() - 1), ((int) data.location
											.getY()));
								}
								if (target.data.location.getX() > data.location
										.getX()) {
									newLoc = new Point(((int) data.location
											.getX() + 1), ((int) data.location
											.getY()));
								}
								map.move(this, newLoc);
								try{
									ms.monsterPositionUpdate(data.id, (int)newLoc.getX(), (int)newLoc.getY());
								}catch(Exception e){
									e.printStackTrace();
								}
								if (target.data.location.getY() < data.location
										.getY()) {
									newLoc = new Point(((int) data.location
											.getX()), ((int) data.location
											.getY() - 1));
								}
								if (target.data.location.getY() > data.location
										.getY()) {
									newLoc = new Point(((int) data.location
											.getX()), ((int) data.location
											.getY() + 1));
								}
								map.move(this, newLoc);
								try{
								ms.monsterPositionUpdate(data.id, (int)newLoc.getX(), (int)newLoc.getY());
								}
								catch(Exception e){
									e.printStackTrace();
								}
							}
						}
					}
				}
			}
		}
	}

	public void attack(ServerPlayerEntity target) {
		int surrounding = 0;
		if (map.get((int) data.location.getX() + 1, (int) data.location.getY()) != null)
			surrounding++;
		if (map.get((int) data.location.getX() - 1, (int) data.location.getY()) != null)
			surrounding++;
		if (map.get((int) data.location.getX(), (int) data.location.getY() + 1) != null)
			surrounding++;
		if (map.get((int) data.location.getX(), (int) data.location.getY() - 1) != null)
			surrounding++;
		if (surrounding >= 2 && data.mana >= 50) {
			data.mana -= 50;
			if (map.get((int) data.location.getX() + 1, (int) data.location
					.getY()) instanceof ServerPlayerEntity)
				((ServerPlayerEntity) (map.get((int) data.location.getX() + 1,
						(int) data.location.getY()))).takeDamage(15,
						data.weapon);
			if (map.get((int) data.location.getX() - 1, (int) data.location
					.getY()) instanceof ServerPlayerEntity)
				((ServerPlayerEntity) (map.get((int) data.location.getX() - 1,
						(int) data.location.getY()))).takeDamage(15,
						data.weapon);
			if (map.get((int) data.location.getX(),
					(int) data.location.getY() + 1) instanceof ServerPlayerEntity)
				((ServerPlayerEntity) (map.get((int) data.location.getX(),
						(int) data.location.getY() + 1))).takeDamage(15,
						data.weapon);
			if (map.get((int) data.location.getX(),
					(int) data.location.getY() - 1) instanceof ServerPlayerEntity)
				((ServerPlayerEntity) (map.get((int) data.location.getX(),
						(int) data.location.getY() - 1))).takeDamage(15,
						data.weapon);
		} else
			target.takeDamage(15, data.weapon);
	}

	public void processMessage(Message m) {

	}
}